﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class frmTheLoaiTaiLieu : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến
        public string maTheLoai = "";
        DataTable dt = new DataTable();
        TextMessageBox box = new TextMessageBox();
        TheLoaiTailieu_BLL tl_bll = new TheLoaiTailieu_BLL();
        private bool isOk = false;
        #endregion

        #region LoadForm
        public frmTheLoaiTaiLieu()
        {
            InitializeComponent();
        }

        private void frmTheLoaiTaiLieu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region Các chức năng

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
        #endregion

        #region các sự kiện hệ thống

        private void frmTheLoaiTaiLieu_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }
        #endregion

        #region Hàm viết thêm
        private void UpdateData()
        {
            if (checkValid())
            {
                TheLoaiTaiLieu_Public tl_public = new TheLoaiTaiLieu_Public();

                if (maTheLoai == "")
                {
                    if (checkTenInsert())
                    {
                        tl_public.MaTheLoai = box.AutoID("TLTL", 4, tl_bll.LayDuLieu());
                        tl_public.TenTheLoai = box.toUpper(txtTenTheLoai.Text);
                        tl_public.GhiChu = txtGhiChu.Text;

                        tl_bll.Insert(tl_public, tl_public, tl_public);
                        CloseForm();
                    }
                }
                else
                {
                    tl_public.MaTheLoai = txtMaTheLoai.Text.Trim();
                    tl_public.TenTheLoai =box.toUpper(txtTenTheLoai.Text);
                    tl_public.GhiChu = txtGhiChu.Text;

                    tl_bll.Update(tl_public, tl_public, tl_public);
                    CloseForm();
                }
            }
        }
        private void CloseForm()
        {
            if (isOk == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }
        private void LoadData()
        {
            if (maTheLoai != "")
            {
                dt = tl_bll.TaoBang(box.sqlCheckExist("DM_TheLoaiTaiLieu","MaTheLoai",maTheLoai));
                DataRow dr=dt.Rows[0];
                txtMaTheLoai.Text=maTheLoai;
                txtTenTheLoai.Text = dr["TenTheLoai"].ToString();
                txtGhiChu.Text = dr["GhiChu"].ToString();
            }
        }
        private void SetInterface()
        {
            txtMaTheLoai.Text = "Mã tự sinh";
            txtTenTheLoai.Text = "";
            txtGhiChu.Text = "";
            txtTenTheLoai.Focus();
            maTheLoai = "";
        }
        private bool checkValid()
        {
            if (txtTenTheLoai.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên thể loại.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenTheLoai.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = tl_bll.TaoBang(box.sqlCheckExist("DM_TheLoaiTaiLieu","TenTheLoai",txtTenTheLoai.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenTheLoai.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtTenTheLoai.Focus();
                return false;
            }
            return true;
        }
        #endregion
    }
}